package com.org.myapplication.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Switch;

import com.org.myapplication.Manager.MagicServiceManager;
import com.org.myapplication.R;

public class CameraActivity extends AppCompatActivity {

    //自动对焦开关状态
    boolean autofocus = false;
    //自动梯形矫正开关状态
    boolean keystone = false;
    //自动幕布识别开关状态
    boolean autoscreen = false;
    //自动避障开关状态
    boolean autooverb = false;

    //自动避障开关状态
    boolean move_focus = false;
    Switch st_autofocus, st_keystone, st_screen, st_overb, st_move_focus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        getdata();
        RefreshUI();
    }

    @Override
    protected void onResume() {
        super.onResume();
        st_autofocus.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                MagicServiceManager.getInstance(this).setAutoFocus(1);
            } else {
                MagicServiceManager.getInstance(this).setAutoFocus(0);
            }
            getdata();
            RefreshUI();
        });


        st_keystone.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                MagicServiceManager.getInstance(this).setAutoKeyStone(1);
            } else {
                MagicServiceManager.getInstance(this).setAutoKeyStone(0);
            }
            getdata();
            RefreshUI();
        });


        st_screen.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                MagicServiceManager.getInstance(this).setAutoScreen(1);
            } else {
                MagicServiceManager.getInstance(this).setAutoScreen(0);
            }
            getdata();
            RefreshUI();
        });


        st_overb.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                MagicServiceManager.getInstance(this).setAutoObstacleavoidance(1);
            } else {
                MagicServiceManager.getInstance(this).setAutoObstacleavoidance(0);
            }
            getdata();
            RefreshUI();
        });

        st_move_focus.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                MagicServiceManager.getInstance(this).setAfSensor(1);
            } else {
                MagicServiceManager.getInstance(this).setAfSensor(0);
            }
            getdata();
            RefreshUI();
        });

    }

    private void initview() {
        st_autofocus = findViewById(R.id.st_autofocus);
        st_keystone = findViewById(R.id.st_keystone);
        st_screen = findViewById(R.id.st_screen);
        st_overb = findViewById(R.id.st_overb);
        st_move_focus = findViewById(R.id.st_move_focus);
    }

    public void getdata() {
        autofocus = (MagicServiceManager.getInstance(this).getAutoFocus() == 1);
        keystone = (MagicServiceManager.getInstance(this).getAutoKeyStone() == 1);
        autoscreen = (MagicServiceManager.getInstance(this).getAutoScreen() == 1);
        autooverb = (MagicServiceManager.getInstance(this).getAutoObstacleavoidance() == 1);
        move_focus = (MagicServiceManager.getInstance(this).getAfSensor() == 1);
    }

    public void RefreshUI() {
        st_autofocus.setChecked(autofocus);
        st_keystone.setChecked(keystone);
        st_screen.setChecked(autoscreen);
        st_overb.setChecked(autooverb);
        st_move_focus.setChecked(move_focus);
    }
}